const app = getApp();
var funcs = require('../../../static/js/funcs.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dealStateList:['是','否'],
    dbList:["无","保证","抵押","质押","保证+抵押","保证+质押"],
    riskList:["正常","关注","次级","可疑","损失"]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log("onload触发")
    var params = decodeURIComponent(options.params);
    var data = JSON.parse(params);
    console.log("跳转后收到的值：")
    console.log(data)
    let uid = data.uid
    let that = this
    console.log("新增不良贷款页面")
    let openid = app.globalData.openid
    funcs.judgeStaff(openid).then(res=>{
      if(res.data.succeed){
        console.log(res.data.data)
        console.log("该员工已注册")
        that.setData({
          staffName:res.data.data.name,
          staffOanum: res.data.data.oanum,
          staffPhone: res.data.data.telnum
        })
        funcs.getLigDetail(openid,uid).then(res=>{
          if(res.data.succeed){
            console.log(res.data.data.lgplus)
            that.setData({
              lgplus:res.data.data.lgplus
            })
          }
        })
      }else{
        app.globalData.loginedFlag = false
        wx.reLaunch({
          url: '/pages/my/my',
        })
        wx.showToast({
          title: "请先绑定",
          icon: 'error',
          duration: 2000
        })
      }
    })
  },


  select_date_func: function (e) {
    let curr = this.data.lgplus
    curr.jkDate = e.detail.value
    console.log('picker发送选择改变，携带值为', e.detail.value);
    this.setData({
      lgplus:curr
    })
  },  

  select_date_func2(e){
    let curr = this.data.lgplus
    curr.dqDate = e.detail.value
    console.log('picker发送选择改变，携带值为', e.detail.value);
    this.setData({
      lgplus:curr
    })
  },
  bindPickerChangeDeal(e){
    let curr = this.data.lgplus
    console.log('picker发送选择改变，携带值为', e.detail.value)
    curr.isPledged = this.data.dealStateList[e.detail.value]
    this.setData({
      lgplus: curr
    })
  },
  bindPickerChangeDb(e){
    let curr = this.data.lgplus
    curr.wayGuarantee = this.data.dbList[e.detail.value]
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      lgplus: curr
    })
  },
  bindPickerChangeRisk(e){
    let curr = this.data.lgplus
    curr.riskStatus = this.data.riskList[e.detail.value]
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      riskStatus: this.data.riskList[e.detail.value]
    })
  },
  clickAdd(){
    let curr = this.data.lgplus
    let currarr = this.data.lgplus.borrowers
    let map ={
      "bname":"",
      "bphone":"",
      "bidcard":""
    }
    currarr.push(map)
    curr.borrowers = currarr
    this.setData({
      lgplus: curr
    })
    console.log(this.data.lgplus)
  },
  clickDelete(){
    let curr = this.data.lgplus
    let currarr = this.data.lgplus.borrowers
    if(currarr.length==1){
      console.log("已经是最后一个了，无法删除")
      return
    }
    currarr.pop()
    curr.borrowers = currarr
    this.setData({
      lgplus: curr
    })
    console.log(this.data.lgplus)
  },

  /**
   * 共同借款人相关
   */
  tinputName(e){
    let curr = this.data.lgplus
    console.log(e)
    let id = e.currentTarget.dataset.id
    let bname = e.detail.value
    let tmpTogether = this.data.lgplus.borrowers
    tmpTogether[id].bname = bname
    curr.borrowers = tmpTogether
    this.setData({
      lgplus: curr
    })
  },
  tinputphone(e){
    let curr = this.data.lgplus
    console.log(e)
    let id = e.currentTarget.dataset.id
    let bPhone = e.detail.value
    let tmpTogether = this.data.lgplus.borrowers
    tmpTogether[id].bphone = bPhone
    curr.borrowers = tmpTogether
    this.setData({
      lgplus: curr
    })
  },
  tinputidcard(e){
    let curr = this.data.lgplus
    console.log(e)
    let id = e.currentTarget.dataset.id
    let bIdcard = e.detail.value
    let tmpTogether = this.data.lgplus.borrowers
    tmpTogether[id].bidcard = bIdcard
    curr.borrowers = tmpTogether
    this.setData({
      lgplus: curr
    })
  },
    //表单信息校验
  todoVerify(){
    //必填信息过滤
    if(this.data.lgplus.uname==null||this.data.lgplus.uidcard==null||this.data.lgplus.jkDate==null||this.data.lgplus.dqDate==null||this.data.lgplus.jkAmount==null||this.data.lgplus.tqAmount==null||this.data.lgplus.tqAmountInterest==null||this.data.lgplus.wayGuarantee==null||this.data.lgplus.riskStatus==null||this.data.lgplus.loanStatus==null||this.data.lgplus.personGuarantee==null||this.data.lgplus.personGuaranteePhone==null||this.data.lgplus.personGuaranteeIdcard==null){
      wx.showToast({
        title: '请补充必填项',
        icon: "error",
        duration: 3000
      })
      return false
    }

    if(this.data.lgplus.staffPhone!=null){
      if(!this.vertifyPhone(this.data.lgplus.staffPhone)||!this.vertifyPhone(this.data.lgplus.personGuaranteePhone)){
        return false;  //员工手机号格式不对
      }
    }
    if(this.data.lgplus.uname==null||!this.validateName(this.data.lgplus.uname)){
      wx.showToast({
        title: '借款人格式异常',
        icon: "error",
        duration: 2000
      })
      return false;
    }
    if(this.data.lgplus.personGuarantee==null||!this.validateName(this.data.lgplus.personGuarantee)){
      wx.showToast({
        title: '保证人格式异常',
        icon: "error",
        duration: 2000
      })
      return false;
    }
    if(this.data.lgplus.pledger==null||!this.validateName(this.data.lgplus.pledger)){
      wx.showToast({
        title: '抵押人格式异常',
        icon: "error",
        duration: 2000
      })
      return false;
    }
    if(this.data.lgplus.uidcard==null||!this.isValidID(this.data.lgplus.uidcard)){
      wx.showToast({
        title: '借款人身份信息错误',
        icon: "none",
        duration: 2000
      })
      return false;
    }
    if(this.data.lgplus.personGuaranteeIdcard==null||!this.isValidID(this.data.lgplus.personGuaranteeIdcard)){
      wx.showToast({
        title: '保证人身份信息错误',
        icon: "none",
        duration: 2000
      })
      return false;
    }
    if(this.data.lgplus.pledgerIdcard==null||!this.isValidID(this.data.lgplus.pledgerIdcard)){
      wx.showToast({
        title: '抵押人身份信息错误',
        icon: "none",
        duration: 2000
      })
      return false;
    }
    if(!this.isNumeric(this.data.lgplus.jkAmount)||!this.isNumeric(this.data.lgplus.tqAmount)||!this.isNumeric(this.data.lgplus.tqAmountInterest)||!this.isNumeric(this.data.lgplus.pgValue)||!this.isNumeric(this.data.lgplus.czValue)){
      wx.showToast({ 
        title: '金额格式异常',
        icon: "error",
        duration: 2000
      })
      return false;
    }

  },
  instNumInput(e){
    let curr = this.data.lgplus
    curr.staffInstNum = e.detail.value
    console.log("staffInstNum>",e.detail.value)
    this.setData({
      lgplus:curr
    })
  },
  stafftelinput(e){
    let curr = this.data.lgplus
    curr.staffPhone = e.detail.value
    console.log("staffPhone>",e.detail.value)
    this.setData({
      lgplus:curr
    })
  },
  instInput(e){
    let curr = this.data.lgplus
    curr.staffInstName = e.detail.value
    this.setData({
      lgplus:curr
    })
    console.log("staffInstName>",e.detail.value)
    
  },
  instInputSub(e){
    let curr = this.data.lgplus
    curr.staffSubInstName = e.detail.value
    this.setData({
      lgplus:curr
    })
    console.log("staffSubInstName>",e.detail.value)
  },
  //必填选项
  inputBuserName(e){
    let curr = this.data.lgplus
    curr.uname = e.detail.value
    this.setData({
      lgplus:curr
    })
    console.log("uname>",e.detail.value)
  },
  inputBuserIdcard(e){
    let curr = this.data.lgplus
    curr.uidcard = e.detail.value
    this.setData({
      lgplus:curr
    })
    console.log("uidcard>",e.detail.value)
  },
  inputjkAmount(e){
    let curr = this.data.lgplus
    curr.jkAmount = e.detail.value
    this.setData({
      lgplus:curr
    })
    console.log("jkAmount>",e.detail.value)
  },
  inputtqAmount(e){
    let curr = this.data.lgplus
    curr.tqAmount = e.detail.value
    this.setData({
      lgplus:curr
    })
    console.log("tqAmount>",e.detail.value)
  },
  inputtqAmountInterest(e){
    let curr = this.data.lgplus
    curr.tqAmountInterest = e.detail.value
    this.setData({
      lgplus:curr
    })
    console.log("tqAmountInterest>",e.detail.value)
  },
  inputloanStatus(e){
    let curr = this.data.lgplus
    curr.loanStatus = e.detail.value
    this.setData({
      lgplus:curr
    })
    console.log("loanStatus>",e.detail.value)

  },
  
  inputpersonGuarantee(e){
    let curr = this.data.lgplus
    curr.personGuarantee = e.detail.value
    this.setData({
      lgplus:curr
    })
    console.log("personGuarantee>",e.detail.value)
  },
  inputpersonGuaranteePhone(e){
    let curr = this.data.lgplus
    curr.personGuaranteePhone = e.detail.value
    this.setData({
      lgplus:curr
    })
    console.log("personGuaranteePhone>",e.detail.value)
  },
  inputpersonGuaranteeIdcard(e){
    let curr = this.data.lgplus
    curr.personGuaranteeIdcard = e.detail.value
    this.setData({
      lgplus:curr
    })
    console.log("personGuaranteeIdcard>",e.detail.value)
  },
  inputpledger(e){
    let curr = this.data.lgplus
    curr.pledger = e.detail.value
    this.setData({
      lgplus:curr
    })
    console.log("pledger>",e.detail.value)
  },

  inputpledgerIdcard(e){
    let curr = this.data.lgplus
    curr.pledgerIdcard = e.detail.value
    this.setData({
      lgplus:curr
    })
    console.log("pledgerIdcard>",e.detail.value)
  },
  inputpgValue(e){
    let curr = this.data.lgplus
    curr.pgValue = e.detail.value
    this.setData({
      lgplus:curr
    })
    console.log("pgValue>",e.detail.value)
  },
  inputczValue(e){
    let curr = this.data.lgplus
    curr.czValue = e.detail.value
    this.setData({
      lgplus:curr
    })
    console.log("czValue>",e.detail.value)
  },
  inputpledgedProperty(e){
    let curr = this.data.lgplus
    curr.pledgedProperty = e.detail.value
    this.setData({
      lgplus:curr
    })
    console.log("pledgedProperty>",e.detail.value)
  },
  inputpledgedAddress(e){
    let curr = this.data.lgplus
    curr.pledgedAddress = e.detail.value
    this.setData({
      lgplus:curr
    })
    console.log("pledgedAddress>",e.detail.value)
  },
  inputpledgedWay(e){
    let curr = this.data.lgplus
    curr.pledgedWay = e.detail.value
    this.setData({
      lgplus:curr
    })
    console.log("pledgedWay>",e.detail.value)
  },
  
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // 手机号格式验证
  vertifyPhone: function (phone) {
    if (!/^1[3456789]\d{9}$/.test(phone)) {
      // 手机号格式不正确，提示用户
      wx.showToast({
        title: '手机格式错误',
        icon: 'error'
      });
      return false;
    }
    return true;
  },
  isValidID:function(id) {
    if (id.length !== 18) {
      return false;
    }
    const weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
    const checkCode = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
    let sum = 0;
    for (let i = 0; i < 17; i++) {
      sum += parseInt(id[i]) * weight[i];
    }
    const index = sum % 11;
    if (checkCode[index] !== id[17].toUpperCase()) {
      return false;
    }
    return true;
  },
  validateName:function (name) {
    // const regex = /^[a-zA-Z\u4e00-\u9fa5]+( [a-zA-Z\u4e00-\u9fa5]+)*$/;
    // return regex.test(name);
    // 收货人姓名校验(准则:姓名为2-8汉字)

      var regu = /^[\u4E00-\u9FA5]{2,8}$/;
      var re = new RegExp(regu);
      if (!re.test(name)) {
        return false;
      }
      return true;
  },
  isNumeric(str) {
    if(str==null)
      return false
    return !isNaN(Number(str));
  },

  modify(e){
    let openid = app.globalData.openid
    let that = this
    console.log(e)
    console.log(this.data.lgplus)
    let input = this.data.lgplus
    input.openid = openid
    if(this.todoVerify()==false)
      return
    wx.showModal({
      title: '请核对信息！',
      content: '确认提交修改吗？',
      complete: (res) => {
        if (res.cancel) {
          console.log("取消")
        }
    
        if (res.confirm) {
          console.log("确认")
          funcs.modifyLg(input).then(res=>{
            if(res.data.succeed){
              console.log("修改成功")
              wx.navigateTo({
                url: '/pages/ss/ssindex/ssindex'
              })
              wx.showToast({
                title: '修改成功',
                icon: "success",
                duration: 2000
              })
            }
          })
        }
      }
    })

  },
  rollback(e){
    let openid = app.globalData.openid
    console.log(e)
    console.log(this.data.lgplus)
    let that = this
    wx.showModal({
      title: '提醒',
      content: this.data.lgplus.status=="不良"? '确认回退到风险状态吗？': '确认回退到正常状态吗？',
      complete: (res) => {
        if (res.cancel) {
          console.log("取消")
        }
    
        if (res.confirm) {
          console.log("确认")
          funcs.rollback(openid,that.data.lgplus.uid,that.data.lgplus.status).then(res=>{
            if(res.data.succeed){
              console.log("状态修改成功")
              wx.navigateTo({
                url: '/pages/ss/ssindex/ssindex'
              })
              wx.showToast({
                title: '状态修改成功',
                icon: "success",
                duration: 2000
              })
            }
          })
        }
      }
    })
  },
  delete(e){
    let openid = app.globalData.openid
    let that = this
    console.log(e)
    console.log(this.data.lgplus)

    wx.showModal({
      title: '提醒',
      content: '确认删除吗？',
      complete: (res) => {
        if (res.cancel) {
          console.log("取消")
        }
    
        if (res.confirm) {
          console.log("确认")
          funcs.deleteLg(openid,that.data.lgplus.uid).then(res=>{
            if(res.data.succeed){
              console.log("删除成功")
              wx.navigateTo({
                url: '/pages/ss/ssindex/ssindex'
              })
              wx.showToast({
                title: '删除成功',
                icon: "success",
                duration: 2000
              })
            }
          })
        }
      }
    })
  },
})